layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var tableIns = table.render({
        elem: '#adminList',
        url: '/main/getManagerList',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 10,
        limits: [10, 15, 20, 25],
        id: "adminListTable",
        cols: [[
            {field: 'loginName', title: '登录用户', align: 'center'},
            {field: 'realName', title: '真实名字', align: 'center'},
            {field: 'mobilePhone', title: '手机号', align: 'center'},
            {field: 'compName', title: '公司', align: 'center'},
            {field: 'email', title: '电子邮箱', align: 'center'},
            {field: 'roleName', title: '权限名称', align: 'center'},
            {field: 'formUserName', title: '绑定的微信号', align: 'center'},
            {title: '操作', templet: '#adminListBar', fixed: "right", align: "center"}
        ]]
    });


    //编辑管理员
    function adminEdit(edit) {
        var index = layui.layer.open({
            title: "修改管理员账号",
            type: 2,
            content: "/managerEdit/index?roleId=" + edit.roleId,
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find(".managerId").attr("value", edit.managerId);
                    body.find(".loginName").val(edit.loginName);
                    body.find(".realName").val(edit.realName);
                    body.find(".mobilePhone").val(edit.mobilePhone);
                    body.find(".email").val(edit.email);
                    form.render();
                }
                ;
            },
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        });
    };


    $(".addbtn").on("click", function () {
        var addindex = layui.layer.open({
            title: "新增管理员账号",
            type: 2,
            content: '/managerAdd/index',
        });
        layui.layer.full(addindex)
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(addindex);
        });
        form.render();
    });

    //列表操作
    table.on('tool(adminList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'edit') { //编辑
            adminEdit(data);
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除此账户？', {icon: 3, title: '提示信息'}, function (index) {
                if (index) {
                    $.get("/main/deleteUser?managerId=" + data.managerId, function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            tableIns.reload();
                        } else {
                            layer.open({
                                title: '温馨提示'
                                , content: result.message
                            });
                            tableIns.reload();
                        }
                        ;
                    });

                } else {
                    tableIns.reload();
                }
                ;
            });
        }
        ;
    });

    // var compId, roleId;
    // $(".li3").on("click", function () {
    //     var comp = $(this).parent().parent();
    //     var a = comp.children().children('span').text();
    //     var b = $(this).text().replace(/(^\s*)|(\s*$)/g, "");
    //
    //     var compId = comp.val();
    //     var roleId = $(this).val();
    //     $(".compId").val(compId);
    //     $(".roleId").val(roleId);
    //     console.log(compId)
    //     console.log(a)
    //     $("#test").val(a + "-" + b);
    //     $('.li3').hide();
    // })
    // $("#test").on("mouseover", function () {
    //     $('.li3').show();
    // })

    // //添加操作
    // form.on('submit(addFormAdd)', function (data) {
    //     $.post("/main/userAdd", data.field, function (rest) {
    //         if (rest.success) {
    //             layer.msg("添加成功");
    //             parent.window.location.reload();
    //         } else {
    //             layer.msg(rest.message);
    //         }
    //     })
    // })
    form.on('submit(changeInfo)', function (data) {
        $.post("/main/updateUserLogin", data.field, function (rest) {
            if (rest.success) {
                layer.msg("编辑成功");
                //parent.window.location.reload();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.layui.table.reload('adminListTable') 
            } else {
                layer.msg(rest.message);
            }
        })
    })

})